package com.ucpaas.paas.web.common.filter;



import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * 权限控制filter
 *
 * @author huangwenjie
 */
@Component
public class AuthorityFilter implements Filter {
    private static final Logger logger = LoggerFactory.getLogger(AuthorityFilter.class);

    private static List<String> whiteList = new ArrayList<String>();

    static {
        whiteList.add("/login");
        whiteList.add("/logout");
        whiteList.add("/forgot/password");
        whiteList.add("/send/code");
        whiteList.add("/register");
        whiteList.add("/check/exist");
        whiteList.add("/favicon.ico");

        whiteList.add("/error");
        whiteList.add("/error/400");
        whiteList.add("/error/403");
        whiteList.add("/error/404");
        whiteList.add("/error/405");
        whiteList.add("/error/408");
        whiteList.add("/error/500");

        whiteList.add("/index/lockscreen");
    }

    /**
     * 是否需要用户登陆验证
     *
     * @param reqURI
     * @return
     */
    private boolean isNeedAuth(String reqURI) {
        // 验证路径是否是白名单
        if (whiteList.contains(reqURI)) {
            return false;
        }

        // 验证是否是对外接口
        if (reqURI.startsWith("/api/")) {
            return false;
        }
        return true;
    }

    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

//        // 去掉项目名
//        String reqURI = request.getRequestURI();
//        reqURI = reqURI.replace("/paas-web", "");
//        // index.html;jsessionid=5D2E7A76FB03359B80368090E8229742
//        if (reqURI.contains(";")) {
//            reqURI = reqURI.substring(0, reqURI.indexOf(";"));
//        }
        filterChain.doFilter(request, response);
//        if (isLogin(request)) {
//            filterChain.doFilter(request, response);
//        } else {// 未登录
//            // 需授权的页面，且未登陆，则返回登陆界面
//            if (isNeedAuth(reqURI)) {
//                response.setStatus(200);
//                ObjectMapper om = new ObjectMapper();
//                PrintWriter out = response.getWriter();
//                ResultVO resultVO = ResultVO.failureWithMsg(ErrorCodeEnums.USER_NOT_LOGIN);
//                out.write(om.writeValueAsString(resultVO));
//                out.flush();
//                out.close();
//            } else {
//                filterChain.doFilter(request, response);
//            }
//        }
    }

    /**
     * 判断是够登陆
     *
     * @param request
     * @return
     */
    private boolean isLogin(HttpServletRequest request) {
//        UserSession user = (UserSession) request.getSession().getAttribute(SessionEnum.SESSION_USER.toString());
//        if (user == null || user.getUid() == null) {
//            return false;
//        }

        return true;
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
    }

}
